package com.umiwe.permission.service.impl;

import com.dingtalk.api.response.OapiDepartmentListResponse;


import com.umiwe.entity.UmiweUserDepartment;
import com.umiwe.permission.mapper.UserDepartmentMapper;
import com.umiwe.permission.service.DingDingService;
import com.umiwe.permission.service.UserDepartmentService;
import com.umiwe.permission.service.UserService;


import com.umiwe.utils.DingDingUrlUtils;
import com.umiwe.utils.Result;
import com.umiwe.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class UserDepartmentServiceImpl implements UserDepartmentService {
    @Autowired
    private DingDingService dingDingService;

    @Resource
    private UserDepartmentMapper userDepartmentMapper;

    @Resource
    private UserService userService;

    /**
     * 添加部门 和 更新部门
     * @return
     */
    @Override
    @Transactional
    public Result getDepartmentList() {
        List linkedList = new LinkedList();
        OapiDepartmentListResponse oapiDepartmentListResponse = dingDingService.oapiDepartmentListRequest(DingDingUrlUtils.DEPARTMENT_LIST.value(), "");
        if(oapiDepartmentListResponse.getDepartment().size() > 0){
            for(int i = 0; i<oapiDepartmentListResponse.getDepartment().size();i++){
                //查询数据 是否存在 存在做修改操作
                boolean departmentId = userDepartmentMapper.getDepartmentId(oapiDepartmentListResponse.getDepartment().get(i).getId().intValue());
                UmiweUserDepartment umiweUserDepartment = new UmiweUserDepartment();
                umiweUserDepartment.setId(oapiDepartmentListResponse.getDepartment().get(i).getId());
                umiweUserDepartment.setDepartmentName(oapiDepartmentListResponse.getDepartment().get(i).getName());
                umiweUserDepartment.setParentId(oapiDepartmentListResponse.getDepartment().get(i).getParentid()); // 父id
                umiweUserDepartment.setIsDelete(1);  //1有效 2无效
                if(departmentId){
                    userDepartmentMapper.updateData(umiweUserDepartment); //修改
                }else{
                    userDepartmentMapper.addDepartment(umiweUserDepartment); //新增
                }
                linkedList.add(umiweUserDepartment);
                //添加 用户 和更新 用户信息
                userService.addUser(oapiDepartmentListResponse.getDepartment().get(i).getId().intValue(),0L,100L);
            }
            //3分钟没更新的数据 定义为 找不到部门 执行无效操作
            ArrayList<Long> longId = userDepartmentMapper.dateDepartmentList();
            if(longId.size() > 0){
                for(Long id: longId){
                    userDepartmentMapper.updateDelete(id);
                }
            }
            return Result.success(linkedList);
        }
        return Result.error(ResultCode.PARAM_IS_INVALID);
    }

    @Override
    public Result getDepartmentDetails(String departmentId) {
        OapiDepartmentListResponse oapiDepartmentListResponse = dingDingService.oapiDepartmentListRequest(DingDingUrlUtils.DEPARTMENT_GET.value(), departmentId);
        return Result.success(oapiDepartmentListResponse.getDepartment());
    }

    /*public OapiDepartmentListResponse utilsDepartment(String value,String departmentId){
        OapiDepartmentListResponse response = new OapiDepartmentListResponse();
        String token = (String)redisTemplate.opsForValue().get("token");
        DingTalkClient client = new DefaultDingTalkClient(Host+value);
        OapiDepartmentListRequest request = new OapiDepartmentListRequest();
        if(departmentId.length() > 0){
            request.setId(departmentId);
        }
        request.setHttpMethod("GET");
        try {
            response = client.execute(request, token);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return response;
    }*/
}
